package org.hfchunya.mutualHelp.modular.system.service.impl;

import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageFactory;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageInfo;
import org.hfchunya.mutualHelp.core.shiro.ShiroKit;
import org.hfchunya.mutualHelp.core.shiro.ShiroUser;
import org.hfchunya.mutualHelp.modular.system.entity.News;
import org.hfchunya.mutualHelp.modular.system.mapper.NewsMapper;
import org.hfchunya.mutualHelp.modular.system.model.params.NewsParam;
import org.hfchunya.mutualHelp.modular.system.model.result.NewsResult;
import org.hfchunya.mutualHelp.modular.system.service.INewsService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 新闻动态信息表 服务实现类
 * </p>
 *
 * @author 付道敏
 * @since 2019-05-05
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Override
    public void add(NewsParam param) {
        News entity = getEntity(param);
        ShiroUser user = ShiroKit.getUser();
        if (user != null) {
            entity.setCreateBy(user.getName());
        }
        this.save(entity);
    }

    @Override
    public void delete(NewsParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(NewsParam param) {
        News oldEntity = getOldEntity(param);
        News newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NewsResult findBySpec(NewsParam param) {
        return null;
    }

    @Override
    public List<NewsResult> findListBySpec(NewsParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NewsParam param) {
        Page pageContext = getPageContext();
        QueryWrapper<News> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NewsParam param) {
        return param.getNewsId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private News getOldEntity(NewsParam param) {
        return this.getById(getKey(param));
    }

    private News getEntity(NewsParam param) {
        News entity = new News();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
